unit clsAssistencia;

interface

uses Dm, dbtables, biblio, sysutils, dbclient, clsEntidade;

type
  TAssistencia = class(TObject)
  public
    Ent_Codigo: string;
    Emp_Codigo: string;
    Ass_Data: TDateTime;
    function Recuperar(Ass_Numero: string): boolean;
    function Retorna_Ultima: string;
    function Retorna_MaiorData: TdateTime;
    function Retorna_MenorData: Tdatetime;
    procedure Gravar(Ass_Numero: string);
    procedure Excluir(Ass_Numero: string);
    procedure Consultar(Data: TDateTime; var cds: TClientDataSet);
  end;

implementation

procedure TAssistencia.Gravar;
var
  tq: TQuery;
begin
  tq := Tquery.Create(DmInt);
  tq.databasename := dmint.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('Select * from Assistencia where Ass_Numero=:a');
  tq.params[0].asstring := Ass_Numero;
  tq.open;
  if tq.eof then begin
    tq.close;
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('Insert into Assistencia values (' +
      ':k_Ass_Numero,' +
      ':k_Ent_Codigo,' +
      ':k_Emp_Codigo,' +
      ':k_Ass_Data  )');
  end else begin
    tq.close;
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('Update Assistencia set     ' +
      'Ent_Codigo=:k_Ent_Codigo,' +
      'Emp_Codigo=:k_Emp_Codigo,' +
      'Ass_Data  =:k_Ass_Data   ' +
      'where                    ' +
      'Ass_Numero =:k_Ass_Numero');
  end;
  tq.parambyname('k_Ass_Numero').asstring := Ass_Numero;
  tq.parambyname('k_Ent_Codigo').asstring := Ent_Codigo;
  tq.parambyname('k_Emp_Codigo').asstring := Emp_Codigo;
  tq.parambyname('k_Ass_Data').asdatetime := Ass_Data;
  tq.execsql;
  tq.close;
  tq.Free;
end;

procedure TAssistencia.Excluir;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('Delete from Assistencia where Ass_Numero=:a');
  tq.params[0].asstring := Ass_Numero;
  tq.execsql;
  tq.close;
  tq.Free;
end;

function TAssistencia.Recuperar;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('Select * from Assistencia where Ass_Numero=:a');
  tq.params[0].asstring := Ass_Numero;
  tq.open;
  if tq.eof then result := false else result := true;
  Ent_Codigo := tq.fieldbyname('Ent_Codigo').asstring;
  Emp_Codigo := tq.fieldbyname('Emp_Codigo').asstring;
  Ass_Data := tq.fieldbyname('Ass_Data').asdatetime;
  tq.close;
  tq.free;
end;

function TAssistencia.Retorna_Ultima;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('Select max(Ass_Numero) ultima from Assistencia');
  tq.open;
  if tq.fieldbyname('ultima').asstring = '' then
    result := '1'
  else
    result := inttostr(tq.fieldbyname('ultima').asinteger + 1);
  tq.close;
  tq.Free;
end;

function TAssistencia.Retorna_MaiorData;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('Select max(Ass_Data) as maior from Assistencia');
  tq.open;
  if tq.eof then
    result := 0
  else
    result := tq.fieldbyname('maior').asdatetime;
  tq.close;
  tq.Free;
end;

function TAssistencia.Retorna_MenorData;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('Select min(Ass_Data) as menor from Assistencia');
  tq.open;
  if tq.eof then
    result := 0
  else
    result := tq.fieldbyname('menor').asdatetime;
  tq.close;
  tq.Free;
end;

procedure TAssistencia.Consultar;
var
  tq: TQuery;
  entidade: TEntidade;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('Select * from Assistencia where Ass_Data>=:a order by Ass_Data,Ass_Numero');
  tq.params[0].asdatetime := Data;
  tq.open;
  entidade := TEntidade.Create;
  while not tq.eof do begin
    cds.Insert;
    cds.fieldbyname('Ass_Numero').asstring := tq.fieldbyname('Ass_Numero').asstring;
    Entidade.Recuperar(tq.fieldbyname('Ent_Codigo').asstring);
    cds.fieldbyname('Entidade').asstring := entidade.Ent_nome;
    cds.fieldbyname('Emp_Codigo').asstring := tq.fieldbyname('Emp_Codigo').asstring;
    if tq.fieldbyname('Ass_Data').asdatetime <> 0 then
      cds.fieldbyname('Data').asstring := tq.fieldbyname('Ass_Data').asstring;
    cds.Post;
    tq.next;
  end;
  cds.first;
  entidade.free;
  tq.close;
  tq.free;
end;

end.
